package com.toocms.jinjuhang.dialog.order_cancel

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.system.GetReasonData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class OrderCancelModel(val dialog: OrderCancelDialog) {
    val titleObservableField = ObservableField<String>()

    val items = ObservableArrayList<OrderCancelItemModel>()
    val itemBinding =
        ItemBinding.of<OrderCancelItemModel>(BR.orderCancelItemModel, R.layout.item_order_cancel)

    val confirmClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
//        dialog?.onConfirmClickListener?.invoke(dialog.tag, dialog.type, selectedReason())
        val url: String
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["order_id"] = dialog.orderId
        if (Constants.Extra.REASON_TYPE_APPLY_SALES == dialog.type || dialog.orderGoodsId.isNullOrEmpty()) {
            //TODO 取消订单
            url = Urls.OrderInfo.CANCEL_ORDER
            params["reason"] = selectedReason()
        } else {
            //TODO 申请退款
            url = Urls.OrderInfo.APPLY_REFUND
            params["order_goods_id"] = dialog.orderGoodsId
            params["apply_refund_reason"] = selectedReason()
        }
        dialog.requestData(url, params, resultClass = String::class.java, result = {
            dialog?.onSucceedListener?.invoke(dialog.tag)
            dialog?.dismiss()
        })
    })

    init {
        val title =
            if (Constants.Extra.REASON_TYPE_APPLY_SALES == dialog.type || dialog.orderGoodsId.isNullOrEmpty()) StringUtils.getString(
                R.string.str_select_order_cancel_hint
            )
            else StringUtils.getString(R.string.str_select_apply_sales_hint)
        titleObservableField.set(title)
        val params = mutableMapOf<String, String?>()
        params["type"] = dialog.type ?: Constants.Extra.REASON_TYPE_CANCELLATION_REASONS
        dialog.requestData(Urls.System.GET_REASON,
            params,
            resultClass = GetReasonData::class.java,
            result = {
                items.clear()
                it.list?.forEachIndexed { index, item ->
                    items += OrderCancelItemModel(
                        this@OrderCancelModel,
                        ObservableField(item.reason),
                        ObservableBoolean(0 == index)
                    )
                }
            })
    }

    private fun selectedReason(): String? {
        var result: String? = null
        items.forEach {
            if (it.isSelectedObservableBoolean.get()) {
                result = it.titleObservableField.get()
            }
        }
        return result
    }

}